#include <iostream>
#include <cstdlib>
#include <ctime>
#include "tbb.h"
#include "dft_inplace/dft.h"
#include "dft_inplace_tbb/dft.h"

using namespace std;

bool TestTBBDftInplace(int n) {
  cout << "TestTBBDftInplace, n=" << n << endl;

  complex<float>* x = new complex<float>[n];
  complex<float>* gold = new complex<float>[n];

  FillRandom(0.f, 1.f, n, x);

  for (int i = 0; i < n; ++i)
    gold[i] = x[i];
  clock_t t = clock();
  DftInplace(gold, n);
  cout << "\tDftInplace time: " << (clock() - t) * 1000 / CLOCKS_PER_SEC << " ms\n";

  t = clock();
  tbbimpl::DftInplace(x, n);
  cout << "\ttbbimpl::DftInplace time: " << (clock() - t) * 1000 / CLOCKS_PER_SEC << " ms\n";

  float err = RelativeError(gold, x, n);
  bool ok = err < 1e-2f;;
  if (ok)
    cout << "\tpassed\n";
  else {
    cout << "\terror: " << err << endl;
    cout << "\tFAILED\n";
  }
  cout << endl;

  delete[] x;
  delete[] gold;
  return ok;
}


int main(int argc, char** argv) {
  tbb::task_scheduler_init init;

  bool ok = true;
  for (int n = 128; n < (1 << 20); n *= 2) {
    bool testok = TestTBBDftInplace(n);
    ok = ok && testok;
  }
  if (ok) 
    cout << "all tests passed\n";
  else
    cout << "there are FAILED tests\n";

  int a;
  cin >> a;

  return 0;
}